package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Taskplan;
import com.init.engineering.service.TaskplanService;
import com.init.engineering.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/taskplan")
public class TaskplanController {
    @Autowired
    private TaskplanService taskplanService;

    @PostMapping("/all")
    public PageResult findTaskplanByPages(@RequestBody QueryPageBean queryPageBean){
        System.out.println("进入BranchController");
        PageResult stagesByPage = taskplanService.findTaskplanByPage(queryPageBean);
        return stagesByPage;
    }


    @PostMapping("/add")
    public Result addTaskplan(@RequestBody Taskplan taskplan){

        //随机ID
        taskplan.setTid(UUIDUtil.GetID());
        //获取系统更新时间
        taskplan.setUpdatetime(new Date());
        int row = taskplanService.saveTaskplan(taskplan);
        Result result = new Result();
        if(row > 0){
            result.setMessage("添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("添加失败，请重试");
        }
        return result;
    }


    @GetMapping("/delete/{tid}")
    public Result deleteTaskplan(@PathVariable("tid") String tid){
        int row = taskplanService.deleteTaskplan(tid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("删除失败，请重试");
        }
        return result;
    }


    @GetMapping("/findTaskplanByTid")
    public Result findTaskplanByTid(String tid){
        Taskplan taskplan = taskplanService.findTaskplanByTid(tid);
        Result result = new Result();
        if (taskplan != null){
            result.setFlag(true);
            result.setMessage("回显成功");
            result.setData(taskplan);
        }else{
            result.setFlag(false);
            result.setMessage("数据回显失败");
        }
        return result;
    }


    @PostMapping("/update")
    public Result updateTaskplan(@RequestBody Taskplan taskplan){
        taskplan.setUpdatetime(new Date());
        int row = taskplanService.updateTaskplan(taskplan);
        Result result = new Result();
        if(row > 0){
            result.setMessage("更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("更新失败，请重试");
        }
        return result;
    }

    @RequestMapping("/findTaskplans")
    public Result findTaskplans(){
        List<Taskplan> taskplans = taskplanService.findTaskplans();
        Result result = new Result();
        if(taskplans != null){
            result.setData(taskplans);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }
}
